<?php
/**
 * 后台菜单模型
 *
 * PHP version 5
 *
 * @package    Shop
 * @subpackage Models
 * @author     Scott Ye <scott.yell@gmail.com>
 * @copyright  2010 Xingchangxinda Inc.
 * @version    SVN: $Id: adminmenu.php 75 2010-10-22 07:28:58Z scott $
 */

namespace Project\Apps\Ecga\Library;

use \Project\Apps\Ecga\Application;

/**
 * 后台菜单模型
 *
 * $Id: adminmenu.php 75 2010-10-22 07:28:58Z scott $
 *
 * @package    Shop
 * @subpackage Models
 * @author     Scott Ye <scott.yell@gmail.com>
 * @copyright  2010 Xingchangxinda Inc.
 * @version    Release: 0.1
 */
class AdminMenu
{

    /**
     * 取得菜单
     *
     * @return array 菜单数组 array('controller' => '', 'action' => '', 'name' => '', 'group' => '')
     * @author Scott Ye
     */
    public static function parseMenuFile()
    {
        $reader = new \XMLReader();
        assert($reader->xml(file_get_contents(self::_getMenuFile())));

        $menus = array();
        while ($reader->read()) {
            if ($reader->nodeType == \XMLReader::ELEMENT) {
                switch ($reader->name) {
                case 'group':
                    $group = $reader->getAttribute('name');
                    break;
                case 'item':
                    $controller = $reader->getAttribute('controller');
                    $action = $reader->getAttribute('action');
                    $name = $reader->readString();
                    $menus[] = array(
                        'controller' => $reader->getAttribute('controller'),
                        'action'     => $reader->getAttribute('action'),
                        'name'       => $reader->readString(),
                        'group'      => $group);
                    break;
                default:
                    break;
                }
            }
        }
        $reader->close();

        return $menus;
    }

    /**
     * 取菜单文件名
     *
     * @return string 菜单文件名
     * @author Scott Ye
     */
    private static function _getMenuFile()
    {
        $filename = Application::getInstance()->getPath() . '/config/adminmenu.xml';
        assert(is_file($filename));

        return $filename;
    }
}
